package org.pi.common.matrix.indices

class DefaultMultidimensionalIndices(private val dimension: Seq[Int]) extends MultidimensionalIndices {
  override final val length = dimension.foldLeft(1)( (a,b) => a*b )
  private val ndims = dimension.length
  final val offsets = initOffsets()

  private final def initOffsets() = {
	var offsets = new Array[Int](ndims)
	offsets(0) = 1
	for (k <- 1 until ndims) 	{
	  offsets(k) = offsets(k-1) * dimension(k-1)
	}
	offsets
  }

  override final def getMin():Int = 0
  override final def getMax():Int = length-1
  override final def hasDenseIndices():Boolean = true
  @inline
  override final def apply(i1: Int): Int = i1
  @inline
  override final def apply(i1: Int, i2: Int): Int = i1+offsets(1)*i2
  @inline
  override final def apply(i1: Int, i2: Int, i3: Int): Int = i1+offsets(1)*i2+offsets(2)*i3
  override final def size: Seq[Int] = dimension
  override final def dimCount: Int = ndims
  override final def equals(i: MultidimensionalIndices): Boolean = {
    if (dimCount != i.dimCount) false
    else {
      var ret: Boolean = true
      for(d <- 0 until dimCount) {
        ret &= ( size(d) == i.size(d))
      }
      ret
    }
  }
}
